var async = require("async")
module.exports = function(app) {
	return new DBRemote(app);
};
var DBRemote = function(app) {
	this.app = app
	DBRemote.app = app
    DBRemote.dbService = this.app.get("dbService")
}

var local = {}




//获取总库存
DBRemote.prototype.getTotalInventory = function(gameType,cb) {
	DBRemote.dbService.db.hgetall("game:gold:inventory:"+gameType,function(err,data) {
		if(err || !data){
			cb(true,{})
		}else{
			cb(true,data)
		}
	})
}
//设置总库存
DBRemote.prototype.setTotalInventory = function(uid,gameType,level,value,cb) {
	DBRemote.dbService.db.hset("game:gold:inventory:"+gameType,level,value,function(err,data) {
		if(err){
			cb(false,err)
		}else{
			var info = JSON.stringify({type : "setTotalInventory",uid : uid,gameType : gameType,level : level,value : value,curValue: data,time : (new Date()).getTime()})
			DBRemote.dbService.db.rpush("gameServer:operatingRecord",info,function(err) {
				if(!err){
					DBRemote.dbService.db.ltrim("gameServer:operatingRecord",-100,-1)
				}
			})
			cb(true)
		}
	})
}
//获取个人库存列表
DBRemote.prototype.getSingleInventory = function(cb) {
	DBRemote.dbService.db.hgetall("game:singleInventory",function(err,data) {
		if(!err && data){
			cb(true,data)
		}else{
			cb(true,{})
		}
	})
}
//设置个人库存
DBRemote.prototype.setSingleInventory = function(uid,targetUid,value,cb) {
	DBRemote.dbService.db.hset("game:singleInventory",targetUid,value,function(err,data) {
		if(!err){
			var info = JSON.stringify({type : "setSingleInventory",uid : uid,targetUid : targetUid,value : value,time : (new Date()).getTime()})
			DBRemote.dbService.db.rpush("gameServer:operatingRecord",info,function(err) {
				if(!err){
					DBRemote.dbService.db.ltrim("gameServer:operatingRecord",-100,-1)
				}
			})
			cb(true)
		}else{
			cb(false,err)
		}
	})
}
//清除个人库存
DBRemote.prototype.cancelSingleInventory = function(uid,targetUid,cb) {
	DBRemote.dbService.db.hdel("game:singleInventory",targetUid,function(err,data) {
		if(!err && data){
			var info = JSON.stringify({type : "cancelSingleInventory",uid : uid,targetUid : targetUid,time : (new Date()).getTime()})
			DBRemote.dbService.db.rpush("gameServer:operatingRecord",info,function(err) {
				if(!err){
					DBRemote.dbService.db.ltrim("gameServer:operatingRecord",-100,-1)
				}
			})
			cb(true)
		}else{
			cb(false,err)
		}
	})
}
//上下分
DBRemote.prototype.changeDiamond = function(uid,targetUid,value,cb) {
	DBRemote.dbService.addItemReason(targetUid,101,value,"管理员上下分:"+uid,function(flag,data) {
		if(flag){
			//后台记录
		    var messageInfo = {
		    	messageType : "changeDiamond",
				admin : uid,
				targetUid : targetUid,
				value : value,
				curValue : data,
				time : (new Date()).getTime()
		    }
		    DBRemote.dbService.writeMessage(JSON.stringify(messageInfo))
		}
		cb(flag)
	})
}
//查询uid
DBRemote.prototype.getPhoneByUid = function(phone,cb) {
	DBRemote.dbService.db.hget("game:phoneMap",phone,function(err,data) {
		if(err || !data){
			cb(false)
		}else{
			cb(true,data)
		}
	})
}

//获取玩家信息
DBRemote.prototype.getBasicInfo = function(uid,cb) {
	DBRemote.dbService.getBasicInfo(uid,cb)
}
//获取管理员操作记录
DBRemote.prototype.getOperatingRecord = function(cb) {
	DBRemote.dbService.db.lrange("gameServer:operatingRecord",-20,-1,function(err,data) {
		if(data){
			cb(true,data)
		}else{
			cb(true,[])
		}
	})
}